package com.core.family.biz.yizhao.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.core.common.gen.BaseQuery;
import com.core.common.gen.PageQuery;
import com.core.family.biz.yizhao.domain.po.YzOrderTake;
import com.core.family.biz.yizhao.mapper.YzOrderTakeMapper;
import com.core.family.biz.yizhao.service.YzOrderPriceService;
import com.core.family.biz.yizhao.service.YzOrderService;
import com.core.family.biz.yizhao.service.YzOrderTakeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

import com.core.common.gen.BaseServiceImpl;
import org.springframework.util.CollectionUtils;


/**
 * @author honglei
 * @version 1.0
 * @time: 2023-7-1 8:26:43
 * @Description 服务实现层 一招接单表
 */
@Slf4j
@Service
public class YzOrderTakeServiceImpl extends BaseServiceImpl<YzOrderTakeMapper, YzOrderTake> implements YzOrderTakeService {

    @Autowired
    YzOrderPriceService priceService;
    @Autowired
    YzOrderService orderService;

    @Override
    public IPage selectPage(YzOrderTake yzOrderTake, PageQuery<BaseQuery> pageQuery) {
        IPage<YzOrderTake> page = super.selectPage(yzOrderTake, pageQuery);
       fillBean(page.getRecords());
        return page;
    }

    @Override
    public List<YzOrderTake> selectList(YzOrderTake yzOrderTake, BaseQuery queryBean) {
        List<YzOrderTake>  list = super.selectList(yzOrderTake, queryBean);
        fillBean(list);
        return list;
    }

    private void  fillBean(List<YzOrderTake> list){
        if (CollectionUtils.isEmpty(list)){
            return;
        }
        for (YzOrderTake record : list) {
            record.setOrderPrice(priceService.getWithDetail(record.getOrderPriceId()));
            record.setYzOrderDto(orderService.getDetail(record.getOrderId()));
        }

    }

    @Override
    public boolean saveIfNot(YzOrderTake take) {
        LambdaQueryWrapper<YzOrderTake> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(YzOrderTake::getOrderId, take.getOrderId())
                .eq(YzOrderTake::getUserId, take.getUserId());
        YzOrderTake db = getOne(wrapper);
        if (Objects.nonNull(db)) {
            return false;
        }

        return save(take);
    }

    @Override
    public boolean updateByUserIdAndOrderId(int userId, int orderId, String sataus) {

        LambdaUpdateWrapper<YzOrderTake> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(YzOrderTake::getOrderId, orderId)
                .eq(YzOrderTake::getUserId, userId)
                .set(YzOrderTake::getStatus, sataus);
        return update(wrapper);
    }
}

